package com.bluetrum.abmate.adapter;

import android.content.Context;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bluetrum.abmate.R;
import com.bluetrum.abmate.databinding.CardEqBinding;
import com.bluetrum.abmate.eq.EqSetting;
import com.bluetrum.abmate.eq.PresetEqSetting;

import java.util.Arrays;
import java.util.List;

public class EqAdapter extends RecyclerView.Adapter<EqAdapter.ViewHolder>
        implements View.OnLongClickListener {

    private Context mContext;

    private int mSpanCount;
    private int mParentWidth;

    private boolean mSelectMode;

    private List<EqSetting> mEqSettings;
    private byte[] mCurrentEqSetting;

    private boolean settingMatches;
    private int matchIndex;

    private OnItemClickListener mOnItemClickListener;

    public EqAdapter(Context context, List<EqSetting> eqSettings, int spanCount) {
        this.mContext = context;
        this.mSpanCount = spanCount;
        this.mEqSettings = eqSettings;
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.mOnItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        mParentWidth = parent.getWidth();

        CardEqBinding binding = CardEqBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        // 正方形
        int edge = mParentWidth / mSpanCount;
        holder.containerView.getLayoutParams().width = edge;
        holder.containerView.getLayoutParams().height = edge;

        holder.containerView.setOnClickListener(this::onClickCard);
        holder.containerView.setOnLongClickListener(this);

        // 配置删除按键
        holder.deleteButton.setOnClickListener(this::onClickDelete);
        // 默认隐藏
        holder.deleteButton.setVisibility(View.GONE);

        // 是否添加按键
        boolean isAddButton = !(isSelectMode() || settingMatches) && (position == mEqSettings.size());

        if (!isAddButton) {
            EqSetting eqSetting = mEqSettings.get(position);
            if (eqSetting.isCustom()) { // 自定义
                holder.eqNameTextView.setText(eqSetting.getName());
                // 选择模式下，如果没有设置匹配，则显示删除按钮
                if (isSelectMode() && position != matchIndex) {
                    holder.deleteButton.setVisibility(View.VISIBLE);
                }
            } else { // 预设
                holder.eqNameTextView.setText(eqSetting.getNameResId());
            }
            holder.containerView.setTag(eqSetting);
            holder.deleteButton.setTag(eqSetting);
            // 设置字体大小
            holder.eqNameTextView.setTextSize(
                    TypedValue.COMPLEX_UNIT_PX,
                    mContext.getResources().getDimension(R.dimen.eq_setting_name_default)
            );

            // EQ是否当前设置，是的话高亮
            if (position == matchIndex) {
                holder.contentView.setBackgroundResource(R.color.eq_setting_current);
            } else {
                holder.contentView.setBackgroundResource(R.color.eq_setting_default);
            }
        } else {
            holder.eqNameTextView.setText(R.string.function_equalizer_custom_add);
            holder.containerView.setTag(PresetEqSetting.EQ_SETTING_ADD);
            holder.deleteButton.setTag(PresetEqSetting.EQ_SETTING_ADD);
            // 设置字体大小
            holder.eqNameTextView.setTextSize(
                    TypedValue.COMPLEX_UNIT_PX,
                    mContext.getResources().getDimension(R.dimen.eq_setting_name_add)
            );
        }
    }

    @Override
    public int getItemCount() {
        return mEqSettings != null
                ? mEqSettings.size() + (isSelectMode() || settingMatches ? 0 : 1) // 选择模式和有匹配设置时不显示添加
                : 0;
    }

    public void setSpanCount(int spanCount) {
        this.mSpanCount = spanCount;
        notifyDataSetChanged();
    }

    private void checkSettings() {
        settingMatches = false;
        matchIndex = -1;

        if (mEqSettings != null) {
            for (int i = 0; i < mEqSettings.size(); i++) {
                byte[] eqSetting = mEqSettings.get(i).getGains();

                if (Arrays.equals(eqSetting, mCurrentEqSetting)) {
                    matchIndex = i;
                    settingMatches = true;
                    break;
                }
            }
        }
    }

    public void setEqSettings(List<EqSetting> eqSettings) {
        this.mEqSettings = eqSettings;
        checkSettings();
        notifyDataSetChanged();
    }

    // TODO: 考虑复位
    public void setCurrentEqSetting(byte[] currentEqSetting) {
        this.mCurrentEqSetting = currentEqSetting;
        checkSettings();
        notifyDataSetChanged();
    }

    public boolean isSelectMode() {
        return mSelectMode;
    }

    public void setSelectMode(boolean selectMode) {
        this.mSelectMode = selectMode;
        notifyDataSetChanged();
    }

    private void onClickCard(View v) {
        if (mOnItemClickListener != null && !isSelectMode()) {
            EqSetting eqSetting = (EqSetting) v.getTag();
            mOnItemClickListener.OnItemClick(eqSetting);
        }
    }

    private void onClickDelete(View v) {
        if (mOnItemClickListener != null && isSelectMode()) {
            EqSetting eqSetting = (EqSetting) v.getTag();
            mOnItemClickListener.onItemWillBeDeleted(eqSetting);
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (mOnItemClickListener != null) {
            EqSetting eqSetting = (EqSetting) v.getTag();
            mOnItemClickListener.onItemLongClick(eqSetting);
            return true;
        }
        return false;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        View containerView;
        View contentView;
        TextView eqNameTextView;
        ImageButton deleteButton;

        public ViewHolder(final CardEqBinding binding) {
            super(binding.getRoot());
            containerView = binding.getRoot();
            contentView = binding.contentView;
            eqNameTextView = binding.eqNameTextView;
            deleteButton = binding.deleteButton;
        }
    }

    public interface OnItemClickListener {
        void onItemWillBeDeleted(EqSetting eqSetting);
        void OnItemClick(EqSetting eqSetting);
        void onItemLongClick(EqSetting eqSetting);
    }

}
